﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ImmovablesManager
{
    /// <summary>
    /// Interaction logic for RealtorsFormEdit.xaml
    /// </summary>
    public partial class RealtorsFormEdit : Window
    {
        private bool _isAdded;
        private Guid _idTRealtorEdit;
        private User _realtorEdit;

        public RealtorsFormEdit(bool isAdded,Guid idTRealtorEdit)
        {
            InitializeComponent();
            _isAdded = isAdded;

            if (!_isAdded) // данные на редактирование
            {
                _idTRealtorEdit = idTRealtorEdit;
                _realtorEdit = AppController.ImmoRepository.GetUserById(_idTRealtorEdit);
                FirstNameTBx.Text = _realtorEdit.UserFirstName;
                LastNameTBx.Text = _realtorEdit.UserLastName;
                ThirdNameTBx.Text = _realtorEdit.UserThirdName;
                AdvNameTBx.Text = _realtorEdit.UserOrganization;
                EmailTBx.Text = _realtorEdit.UserEmail;
                PhoneTBx.Text = _realtorEdit.UserPhoneNumber;
                AddPhoneTBx.Text = _realtorEdit.UserPhoneNumber;
                LoginTBx.Text = _realtorEdit.UserLogin;
                PasswordTBx.Text = _realtorEdit.UserPassword;

            }
        }


        private void CancelRealtorBtn_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void SaveRealtorBtn_Click(object sender, RoutedEventArgs e)
        {
            if (_isAdded)
            {
                var realtorGuid = AppController.ImmoRepository.AddUser(FirstNameTBx.Text, LastNameTBx.Text,
                                         ThirdNameTBx.Text, LoginTBx.Text,PasswordTBx.Text,PhoneTBx.Text,
                                          EmailTBx.Text, "realtor");

                //AppController.ImmoRepository.AddUser(FirstNameTBx.Text,
                //    LastNameTBx.Text, ThirdNameTBx.Text,
                //    LoginTBx.Text, PasswordTBx.Text, PhoneTBx.Text,
                //    EmailTBx.Text, "realtor");

                AppController.realtorsDataGridDataViews.Add(new RealtorsDataGridDataView()
                                                                {
                                                                    RealtorAdditionalPhone = PhoneTBx.Text,
                                                                    RealtorEmail = EmailTBx.Text,
                                                                    RealtorFirstName = FirstNameTBx.Text,
                                                                    RealtorOrganization = AdvNameTBx.Text,
                                                                    RealtorSecondName = LastNameTBx.Text,
                                                                    RealtorThirdName = ThirdNameTBx.Text,
                                                                    RealtorUserName = LoginTBx.Text,
                                                                    RealtorPhone = PhoneTBx.Text,
                                                                    RealtorId = realtorGuid
                                                                });
            }
            else
            {
                AppController.ImmoRepository.EditUser(_idTRealtorEdit, FirstNameTBx.Text, LastNameTBx.Text,
                                                          ThirdNameTBx.Text, PhoneTBx.Text, AddPhoneTBx.Text,
                                                          AdvNameTBx.Text, EmailTBx.Text, LoginTBx.Text,
                                                          PasswordTBx.Text);
                var realtorToEdit = AppController.realtorsDataGridDataViews.Single(realt=>realt.RealtorId == _idTRealtorEdit);
                realtorToEdit.RealtorAdditionalPhone = PhoneTBx.Text;
                realtorToEdit.RealtorEmail = EmailTBx.Text;
                realtorToEdit.RealtorFirstName = FirstNameTBx.Text;
                realtorToEdit.RealtorOrganization = AdvNameTBx.Text;
                realtorToEdit.RealtorSecondName = LastNameTBx.Text;
                realtorToEdit.RealtorThirdName = ThirdNameTBx.Text;
                realtorToEdit.RealtorUserName = LoginTBx.Text;
                realtorToEdit.RealtorPhone = PhoneTBx.Text;
            }

            Close();
        }
    }
}
